
class Rational(n: Int, d: Int) extends Ordered[Rational]{
 
  def compare(that: Rational) = 
    this.numer * this.denom - that.numer * that.denom
  
  private final val g = gcd(n.abs, d.abs)
  
  final val numer: Int = n / g
  final val denom: Int = d / g
  require(d != 0, "d can't be 0!");
  
  implicit def intToRational(x: Int) = new Rational(x)
  
  override def toString() = numer + " / " + denom;
 
  def this(n: Int) = this(n, 1)
  
  def +(that: Rational) =
    new Rational(numer * that.denom + denom * that.numer, that.denom * denom)
  
  def +(x: Int) =
    new Rational(numer + x * denom, denom)
  
  def -(that: Rational) = 
    new Rational(numer * that.denom - denom * that.numer, that.denom * denom)
  
  def -(x: Int) = 
    new Rational(numer  - denom * x, denom)
 
  def *(that: Rational) = {
    val rat1 = 2 / this
    print(rat1)
    new Rational(numer * that.numer, denom * that.denom)
  }
  
  def *(x: Int) = 
    new Rational(numer * x, denom)
  
  def /(that: Rational) = 
    new Rational(numer * that.denom, denom * that.numer)
  
  def /(x: Int) = 
  new Rational(numer, denom * x)
 
  
//  def <=(that: Rational) =
//    numer * that.denom < denom * that.numer
    
  def max(that: Rational) = 
    if (<=(that)) that else this
    
  private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a%b)
  
}